package cn.zysheep;

import cn.zysheep.feign.ProviderFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
@EnableFeignClients("cn.zysheep")
public class CosumerApplication {


    // 开启ribbon负载均衡
    // ribbon不是Spring cloud alibaba 的组件，是netflix公司提供的
    @LoadBalanced
    @Bean
    public RestTemplate  restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args) {
         ConfigurableApplicationContext applicationContext = SpringApplication.run(CosumerApplication.class, args);
         Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.err::println);
         String property = applicationContext.getEnvironment().getProperty("user.name");
         System.err.println("user.name:"+property);
    }

    @Api(tags = "消费方接口API")
    @RequestMapping("/api")
    @RestController
    @RefreshScope  //支持Nacos的动态刷新功能
    public class TestController{

        @Autowired
        private ProviderFeignClient providerFeignClient;

        @Value("${user.name}")
        private String name;

        private final  RestTemplate restTemplate;

        @Autowired
        private  DiscoveryClient discoveryClient;

        @Autowired
        public TestController(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }



        @GetMapping("/consumer/{str}")
        @ApiOperation("restTemplate远程调用测试")
        public String echo(@PathVariable String str){

            return restTemplate.getForObject("http://service-provider/provider/"+str,String.class);
        }

        @ApiOperation("查看provider的实例信息")
        @GetMapping("/consumer/getInstances")
        public List<ServiceInstance> instances(){
            List<ServiceInstance> provider = discoveryClient.getInstances("provider");
            return provider;
        }

        @GetMapping("/consumer/test")
        public String testConfig(){
            //测试nacos config
            return name;
        }


        @ApiOperation("Feign远程调用测试")
        @GetMapping("/consumer/testFeignClient")
        public String testFeignClient(){
            //测试nacos config
            String testFeignClient = providerFeignClient.echo("TestFeignClient");

            return testFeignClient;
        }


    }

}
